// Copyright (c) 2011 Vratislav Cermak (vratacermak@gmail.com)
//  
// This file is part of SrtSync Project. See http://code.google.com/p/srtsync/ for more information.
//  
// SrtSync is an open source project. Using of SrtSync is free of charge.
// It is distributed under GNU General Public License version 3 (GPLv3).
// GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
//  
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.

using System;
using System.Linq;
using SrtSync.Core.Framework;
using SrtSync.Core.Notifications;

namespace SrtSync.Core.Controllers
{
    public class ApplicationController : IApplicationController
    {
        private readonly IDialogs _dialogs;
        private readonly IApplication _application;
        private readonly IApplicationView _view;

        public ApplicationController(IApplication application, IDialogs dialogs, IApplicationView view)
        {
            _dialogs = dialogs;
            _application = application;
            _view = view;
            
            _view.Register(this);
        }

        public void Process(Notification<ApplicationViewNotificationTypes> data)
        {
            data.Type.CheckValue();

            switch (data.Type)
            {
                case ApplicationViewNotificationTypes.Synchronize:
                    string path;
                    if (_application.Left.Entries.Count() != _application.Right.Entries.Count())
                    {
                        _dialogs.ShowError("Number of rows must be equal in both editors.");
                    }
                    else if (_dialogs.SaveFile(out path))
                    {
                        try
                        {
                            _application.Synchronize(path);
                            _dialogs.ShowInfo("Synchronization successfully completed.");
                        }
                        catch (Exception e)
                        {
                            _dialogs.ShowError("Could not synchronize.", e);
                        }
                    }
                    break;
                case ApplicationViewNotificationTypes.About:
                    _dialogs.ShowAboutDialog();
                    break;
                case ApplicationViewNotificationTypes.Quit:
                    if (_application.Left.Modified || _application.Right.Modified)
                    {
                        if (_dialogs.ShowQuestion("File(s) has been modified, do you want to save changes?"))
                        {
                            if (_application.Left.Modified)
                            {
                                try
                                {
                                    _application.Left.Save();
                                }
                                catch (Exception e)
                                {
                                    _dialogs.ShowError(String.Format("Could not save file '{0}'.", _application.Left.Path), e);
                                }
                            }
                            if (_application.Right.Modified)
                            {
                                try
                                {
                                    _application.Right.Save();
                                }
                                catch (Exception e)
                                {
                                    _dialogs.ShowError(String.Format("Could not save file '{0}'.", _application.Right.Path), e);
                                }
                            }
                        }
                    }
                    _application.Terminate();
                    break;
            }
        }
    }
}